package com.rfid.a252robot;

import android.app.Application;

import com.rfid.a252robot.config.AcsConfig;
import com.rfid.common.log.LogUtils;
import com.rfid.db.bean.DaoMaster;
import com.rfid.db.bean.DaoSession;
import com.rfid.manager.ConfigManager;
import com.rfid.manager.DoorBoxManager;
import com.rfid.util.PreferenceUtil;
import com.spectre.generate.exception.ConfigFileNotFoundException;

import org.greenrobot.greendao.database.Database;

import java.io.File;

public class App extends Application {

    private static App mApp;
    private DaoSession mDaoSession;
    @Override
    public void onCreate() {
        super.onCreate();
        mApp = this;
    }

    public static App getApp() {
        return mApp;
    }

    public void init() {

        try {
            com.spectre.generate.ConfigManager.INSTANCE.init();
        } catch (ConfigFileNotFoundException e) {
            e.printStackTrace();
        }
        String path = this.getExternalFilesDir("").getPath();
        path = "/sdcard/robot";
        LogUtils.init(this,path);
        LogUtils.v("init");
        PreferenceUtil.init(this);

        DaoMaster.OpenHelper openHelper = new DaoMaster.DevOpenHelper(this,path + "/robot.db");
        Database database = openHelper.getWritableDb();
        mDaoSession = new DaoMaster(database).newSession();
        AcsConfig config = com.spectre.generate.ConfigManager.INSTANCE.getAcsConfig();
        new Thread(new Runnable() {
            @Override
            public void run() {
                ConfigManager.INSTANCE.initTcpClient(config.getIp(), config.getPort(), config.getDeviceNo());
            }
        }).start();
    }


    public DaoSession getDaoSession() {
        if(mDaoSession == null) {
            init();
        }
        return mDaoSession;
    }

    public File getCacheDir() {
        return  getExternalCacheDir();
    }
}

